<?php

class ContentsController extends AppController {

  public $helpers = array('Html', 'TWSForm');
  public $components = array('Session', 'Util','Upload');
  public $name = 'Contents';
  public $map = array();

  //TODO - Keywords and description have to be filled out

  function __construct($request=null, $response=null) {
    parent::__construct($request, $response);
    $this->map = Configure::read('App.contentPage');
  }

 function message(){}

  function home() {
    //$this->loadModel('Album');
    //$this->layout ='home';
    $this->set("title_for_layout", "Allied Caterers Limited of Trinidad and Tobago");
    $this->set("meta_keywords", "Allied Caterers Trinidad Tobago catering services");
    $this->set("meta_description", "Allied Caterers Limited is one of Trinidad and Tobago's leading contemporary caterers offering a wide range of catering services.");

    //$this->Content->id = $this->map['aboutus'];
    $this->Content->id = 'Home1';
    $home1 = $this->Content->read();
    $this->Content->id = 'Home2';
    $home2 = $this->Content->read();
    $this->set('home1', $home1);
    $this->set('home2', $home2);

    /*
    //setting Home Page Images
    $adminAlbums = Configure::read('App.administrativeAlbums');
    $this->Album->id = $adminAlbums['Rotating Images']['id'];
    $rotatingImages = $this->Album->Photo->find("all", array("conditions"=>array("Album.id"=>$adminAlbums['Rotating Images']['id']), "order"=>array("Photo.image_order ASC")));
    $this->set('rotatingImages', $rotatingImages);
    */
  }

  function about_us() {
    $this->loadModel('Album');
    $this->set("title_for_layout", "About Allied Caterers Services");
    $this->set("meta_keywords", "Allied Caterers About Trinidad and Tobago");
    $this->set("meta_description", "Learn about Allied Caterers Limited of Trinidad and Tobago.");

    $this->Content->id = 'AboutUsMain';
    $aboutUsMain = $this->Content->read();
    $this->Content->id = 'AboutUsValues';
    $aboutUsValues = $this->Content->read();
    $this->Content->id = 'AboutUsExecChef';
    $aboutUsExecChef = $this->Content->read();
    $this->set('aboutUsMain', $aboutUsMain);
    $this->set('aboutUsValues', $aboutUsValues); 
    $this->set('aboutUsExecChef', $aboutUsExecChef);
    //Setting images
    $adminAlbums = Configure::read('App.administrativeAlbums');
    $this->Album->id = $adminAlbums['Executive Chef']['id'];
    $execChefImage = $this->Album->read();
    $execChefImage = !empty($execChefImage) && !empty($execChefImage['Photo'])  && !empty($execChefImage['Photo'][0]) ? $execChefImage['Photo'][0] : null;
    $execTeamImages = $this->Album->Photo->find("all", array("conditions"=>array("Album.id"=>$adminAlbums['Executive Team']['id']), "order"=>array("Photo.image_order ASC")));
    $this->set('chefImage',$execChefImage);
    $this->set('teamImages',$execTeamImages);
  }

  function sample_menus() {
    $this->set("title_for_layout", "Sample Menus for Allied Caterers Services");
    $this->set("meta_keywords", "Allied Caterers Sample Menus Trinidad and Tobago");
    $this->set("meta_description", "See the sample menus and learn about the delicious options that are offered by Allied Caterers Services.");
    $menu_exists = false;
    if (file_exists(WWW_ROOT . Configure::read("App.sampleMenus"))) {
      $menu_exists = true;
    }
    $this->set("menu_exists", $menu_exists);
  }

  function contact_us() {
    $this->set("title_for_layout", "Contact Allied Caterers in Trinidad and Tobago.");
    $this->set("meta_keywords", "Allied Caterers Trinidad and Tobago contact information");
    $this->set("meta_description", "Contact Allied Caterers in Trinidad and Tobago for your catering needs.");

    $this->Content->id = 'ContactUs';
    $contactUs = $this->Content->read();
    $this->set('contactUs', $contactUs);

  }

  function request_quote() {
    $this->set("title_for_layout", "Request a Quote from Allied Caterers.");
    $this->set("meta_keywords", "Allied Caterers Trinidad and Tobago Quotes");
    $this->set("meta_description", "Request a quotation for catering services at Allied Caterers in Trinidad and Tobago.");
  }

  function admin_home() {
    $this->layout = 'admin';
    $this->set('title_for_layout', 'Allied Caterers Administrative Home Page');
  }

  function admin_index() {
    //Display a list of content pages
    $this->layout = 'admin';
    $this->set('options', $this->Content->find('list', array('fields' => array('Content.id', 'Content.content_description'), 'order' => array('Content.content_description' => 'asc'))));
  }

  function admin_view() {
    $this->layout = 'admin';
    $id = $this->request->data["id"];
    if (!empty($id)) {
      $this->redirect(array('action' => 'edit', 'admin' => true, $id));
    }
  }

  function admin_edit($id = null) {
    $this->layout = 'admin';
    $this->Content->id = $id;
    if ($this->request->is('get')) {
      $this->set('contents', $this->Content->read());
    } else {
      $this->_processAction($this->request, 'edit', $id);
    }
  }

    function admin_sample_menus() {
    $this->layout = 'admin';
    $this->set("title_for_layout", "Sample Menu for Allied Caterers Services");
    //check for existense of menu
    $menu_exists = false;
    if (file_exists(WWW_ROOT . Configure::read("App.sampleMenus"))) {
      $menu_exists = true;
    }
    $this->set("menu_exists", $menu_exists);
  }

   function admin_sample_menus_add(){
    $this->layout = 'admin';
      if($this->request->is('post')){
      $fileUploaded = $this->Upload->uploadFile($this->request->params['form']['ContentFilePath'], WWW_ROOT .'/uploads' );
        if($fileUploaded) {
          $this->set("menu_exists", true);
          $this->Session->setFlash('The sample menu has been uploaded.');
        } else {
          $this->Session->setFlash('An error occurred and the sample menu has not been uploaded.');
        }
    }
    $this->render('admin_sample_menus');
  }

  function admin_sample_menus_delete(){
    $this->layout = 'admin';
      $result = $this->Util->deleteFile(WWW_ROOT . Configure::read('App.sampleMenus') );
      if($result) {
        $this->set("menu_exists", false);
        $this->Session->setFlash('The sample menu has been deleted.');
      }
    $this->render('admin_sample_menus');
  }

  private function _processAction($dataIn, $action='add', $id=null) {
    $data = $this->_processRequestData($dataIn);
    $data['Content']['id'] = $id;
    $this->Content->set($data);
    if ($this->Content->validates()) {
      if ($this->Content->save($data, false)) {
        $this->Session->setFlash('The record was successfully updated.');
        $this->redirect(array('action' => 'index'));
      }
    }
  }

}

?>
